import type { InventoryTransaction } from '../inventory/types';

import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn } from '#/adapter/vxe-table';

import { z } from '#/adapter/form';
import { $t } from '#/locales';

// 定义格式化函数参数类型，兼容VxeTable的formatter参数类型
interface FormatterParams {
  cellValue: any;
}

/**
 * 获取表格列配置
 */
export function useColumns(
  onActionClick: OnActionClickFn<InventoryTransaction>,
): any[] {
  return [
    {
      field: 'transaction_id',
      title: $t('common.id'),
      width: 80,
      align: 'center',
    },
    {
      field: 'transaction_code',
      title: '事务编号',
      width: 180,
      sortable: true,
    },
    {
      field: 'product_code',
      title: '产品编码',
      width: 120,
      sortable: true,
    },
    {
      field: 'product_name',
      title: '产品名称',
      width: 180,
      sortable: true,
    },
    {
      field: 'transaction_type',
      title: '事务类型',
      width: 100,
      sortable: true,
      formatter: ({ cellValue }: FormatterParams) => {
        const typeMap: Record<string, { text: string }> = {
          stock_in: { text: '入库' },
          stock_out: { text: '出库' },
          stock_move: { text: '移动' },
          stock_adjust: { text: '调整' },
          stock_check: { text: '盘点' },
        };
        const typeInfo = typeMap[cellValue || ''] || { text: cellValue || '-' };
        return typeInfo.text;
      },
    },
    {
      field: 'quantity',
      title: '数量',
      width: 100,
      align: 'right',
      sortable: true,
      formatter: ({ cellValue }: FormatterParams) => {
        const num = parseFloat(cellValue || '0');
        return num > 0 ? `+${num}` : num;
      },
    },
    {
      field: 'unit_cost',
      title: '单价',
      width: 100,
      align: 'right',
      sortable: true,
    },
    {
      field: 'warehouse',
      title: '操作仓库',
      width: 120,
      sortable: true,
      formatter: ({ row }) => {
        return row.warehouse?.warehouse_name || '-';
      },
    },
    {
      field: 'location',
      title: '操作库位',
      width: 120,
      sortable: true,
      formatter: ({ row }) => {
        return row.location?.location_name || '-';
      },
    },
    {
      field: 'creator',
      title: '操作人',
      width: 100,
      sortable: true,
    },
    {
      field: 'source_doc_type',
      title: '源文档类型',
      width: 120,
    },
    {
      field: 'source_doc_id',
      title: '源文档ID',
      width: 120,
    },
    {
      field: 'create_time',
      title: $t('system.createTime'),
      width: 180,
      formatter: ({ row }) => {
        return row.create_time
          ? new Date(row.create_time).toLocaleString('zh-CN')
          : '-';
      },
    },
    {
      field: 'action',
      title: '操作',
      width: 100,
      cellRender: {
        name: 'CellOperation',
        attrs: {
          onClick: onActionClick,
        },
        options: [
          {
            code: 'detail',
            text: '查看详情',
            icon: 'EyeOutlined',
          },
        ],
      },
    },
  ];
}

/**
 * 获取搜索表单的字段配置
 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'product_code',
      label: '产品编码',
      rules: z.string().optional(),
    },
    {
      component: 'Input',
      fieldName: 'product_name',
      label: '产品名称',
      rules: z.string().optional(),
    },
    {
      component: 'Select',
      fieldName: 'transaction_type',
      label: '事务类型',
      componentProps: {
        allowClear: true,
        options: [
          { label: '入库', value: 'stock_in' },
          { label: '出库', value: 'stock_out' },
          { label: '移动', value: 'stock_move' },
          { label: '调整', value: 'stock_adjust' },
          { label: '盘点', value: 'stock_check' },
        ],
      },
      rules: z.string().optional(),
    },
    {
      component: 'Input',
      fieldName: 'creator',
      label: '操作人',
      rules: z.string().optional(),
    },
    {
      component: 'DateRangePicker',
      fieldName: 'date_range',
      label: '事务日期范围',
      componentProps: {
        format: 'YYYY-MM-DD',
        placeholder: ['开始日期', '结束日期'],
      },
      rules: z.array(z.any()).length(2).optional(),
    },
  ];
}
